package com.example.mediaplayer

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.media.MediaPlayer
import android.os.Bundle
import android.view.View
import android.widget.ImageButton
import android.widget.SeekBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.util.*


class MainActivity : AppCompatActivity(), View.OnClickListener {
    private val last: ImageButton by lazy { findViewById<ImageButton>(R.id.last) }
    private val play: ImageButton by lazy { findViewById<ImageButton>(R.id.play) }
     private val pause: ImageButton by lazy { findViewById<ImageButton>(R.id.pause) }
    private val next: ImageButton by lazy { findViewById<ImageButton>(R.id.next) }
    private val seekbar: SeekBar by lazy { findViewById<SeekBar>(R.id.seekbar) }
var timer =Timer()
    private val title:TextView by lazy{findViewById(R.id.title)}
    val CTL_ACTION = "org.xr.action.CTL_ACTION"
    val UPDATE_ACTION = "org.xr.action.UPDATE_ACTION"
    var activityReceiver: ActivityREceiver? = null
    var status = 0x11
    var mediaPlayer= MediaPlayer()
    var titleStrs = arrayOf("music1.mp3", "music2.mp3", "music3.mp3","music4.mp3")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        play.setOnClickListener(this)
        pause.setOnClickListener(this)
        last.setOnClickListener(this)
        next.setOnClickListener(this)
        activityReceiver = ActivityREceiver()
        val filter = IntentFilter()
        filter.addAction(UPDATE_ACTION)
        registerReceiver(activityReceiver, filter)
        val intent = Intent(this, MusicService::class.java)
        startService(intent)


//        seekbar.max=mediaPlayer.duration
//        seekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
//            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
//                // 滑动中
//
//            }
//
//            override fun onStartTrackingTouch(seekBar: SeekBar?) {
//                // 开始滑动
//            }
//
//            override fun onStopTrackingTouch(seekBar: SeekBar?) {
//                // 滑动结束
//                mediaPlayer.seekTo(seekbar.progress)
//            }
//        })
//
//        getProgress()


    }

    override fun onClick(v: View) {
        val intent = Intent("org.xr.action.CTL_ACTION")
        when (v.getId()) {
            R.id.play -> {
                intent.putExtra("control", 1)
//                getProgress()
            }
            R.id.pause -> intent.putExtra("control", 2)
            R.id.last -> intent.putExtra("control", 3)
            R.id.next -> intent.putExtra("control", 4)
        }
        sendBroadcast(intent)
    }

    inner class ActivityREceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent) {
            val update: Int = intent.getIntExtra("update", 1)
            val current: Int = intent.getIntExtra("current", -1)
            if (current >= 0) {
                title.setText(titleStrs[current])
            }
            when (update) {
                0x11 -> {
                    play.setImageResource(R.drawable.play)
                    status = 0x11
                    title.setText("")
                }
                0x12 -> {
                    play.setImageResource(R.drawable.pause)
                    status = 0x12
                }
                0x13 -> {
                    play.setImageResource(R.drawable.play)
                    status = 0x13
                }
            }
        }
    }


    companion object {
        const val CTL_ACTION = "org.xr.action.CTL_ACTION"
        const val UPDATE_ACTION = "org.xr.action.UPDATE_ACTION"
    }
//    private fun getProgress() {
//        timer.schedule(object : TimerTask() {
//            override fun run() {
//
//                //获取歌曲的进度
//                var p =intent.getIntExtra("seekbar",1)
//
//                //将获取歌曲的进度赋值给seekbar
//                seekbar.progress = p
//            }
//        }, 0, 1)
//    }
}



//package com.example.mediaplayer
//
//import android.content.BroadcastReceiver
//import android.content.Context
//import android.content.Intent
//import android.media.MediaPlayer
//import androidx.appcompat.app.AppCompatActivity
//import android.os.Bundle
//import android.widget.ImageButton
//import android.widget.SeekBar
//import java.util.*
//
//class MainActivity : AppCompatActivity() {
//
//    private val last: ImageButton by lazy { findViewById<ImageButton>(R.id.last) }
//    private val play: ImageButton by lazy { findViewById<ImageButton>(R.id.play) }
//    private val pause: ImageButton by lazy { findViewById<ImageButton>(R.id.pause) }
//    private val stop: ImageButton by lazy { findViewById<ImageButton>(R.id.stop) }
//    private val next: ImageButton by lazy { findViewById<ImageButton>(R.id.next) }
//    private val seekbar: SeekBar by lazy { findViewById<SeekBar>(R.id.seekbar) }
//    var current:Int = 0
//
//    val musics= arrayOf("music1.mp3","music2.mp3","music3.mp3","music4.mp3")
//    private val mediaPlayer = MediaPlayer()
//    var isFinished:Boolean = false
//    var timer = Timer()
//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_main)
//        initMediaPlayer()
//        var intent :Intent = Intent("org.xr.action.CTL_ACTION")
//
//        //ge qu de bo fang shi chang
//        seekbar.max=mediaPlayer.duration
//        seekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
//            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
//                // 滑动中
//
//            }
//
//            override fun onStartTrackingTouch(seekBar: SeekBar?) {
//                // 开始滑动
//            }
//
//            override fun onStopTrackingTouch(seekBar: SeekBar?) {
//                // 滑动结束
//                mediaPlayer.seekTo(seekbar.progress)
//            }
//        })
//
//        play.setOnClickListener{
//            if(!mediaPlayer.isPlaying){
//                mediaPlayer.start()
//                intent.putExtra("control",1)
//            }
//
//            getProgress()
//
//        }
//        pause.setOnClickListener{
//            if(mediaPlayer.isPlaying){
//                mediaPlayer.pause()
//                isFinished=true
//                intent.putExtra("control",2)
//
//            }
//            seekbar.progress=mediaPlayer.currentPosition
//
//        }
//        last.setOnClickListener{
//            if(mediaPlayer.isPlaying){
//                intent.putExtra("control",3)
//
//                mediaPlayer.pause()
//                isFinished=true
//                if(current==0)
//                {
//                    current=3
//                }
//                else{
//                    current--
//                }
//                initMediaPlayer()
//                mediaPlayer.start()
//                getProgress()
//            }
//        }
//        next.setOnClickListener{
//            if(mediaPlayer.isPlaying){
//                intent.putExtra("control",4)
//
//                mediaPlayer.pause()
//                isFinished=true
//                if(current==3)
//                {
//                    current=0
//                }
//                else{
//                    current++
//                }
//                initMediaPlayer()
//                mediaPlayer.start()
//                getProgress()
//            }
//        }
//        sendBroadcast(intent)
//    }
//
//    private fun initMediaPlayer() {
//        val assetManager = assets
//        isFinished=false
//        var filename:String = musics[current]
//        val fd = assetManager.openFd(filename)
//        mediaPlayer.setDataSource(fd.fileDescriptor,fd.startOffset,fd.length)
//        mediaPlayer.prepare()
//
//        seekbar.progress=mediaPlayer.currentPosition
//    }
//
//    override fun onDestroy() {
//        super.onDestroy()
//        mediaPlayer.stop()
//        mediaPlayer.release()
//    }
//    private fun getProgress() {
//        timer.schedule(object : TimerTask() {
//            override fun run() {
//
//                //获取歌曲的进度
//                val p: Int =mediaPlayer.getCurrentPosition()
//
//                //将获取歌曲的进度赋值给seekbar
//                seekbar.progress = p
//            }
//        }, 0, 1)
//    }
//
//
////    inner class ActivityREceiver: BroadcastReceiver() {
////        override fun onReceive(context: Context, intent: Intent) {
////            var update:Int = intent.getIntExtra("update",1)
////            var current:Int = intent.getIntExtra("current",-1)
////            if(current>=0){
////                //待定，显示歌词可以
////            }
////            when(update){
////                0x11->{
////
////                }
////            }
////        }
////    }
//
//}

